<template>
  <div class="home" v-if="data?.length" @scroll="scroll" ref="listBox">
    <van-pull-refresh v-model="rLoading" @refresh="onRefresh">
      <div class="list-box">
        <div
          class="item-box"
          :style="{ gridRowEnd: 'span ' + (item.height + 113 - 1) }"
          v-for="(item, index) in data"
          :key="index"
        >
          <div
            :style="{
              backgroundImage: `url(${getUrl(item.imgUrl)})`,
              backgroundSize: '100% 100%',
              backgroundRepeat: 'no-repeat',
              height: item.height + 'px',
            }"
            class="img"
            @click="startPlayback(item)"
          >
            <div class="player" v-if="item.type == 'video'">
              <van-icon name="play" />
            </div>
          </div>

          <div class="info-box">
            <div class="title-box">{{ item.title }}</div>
            <span class="game-name">{{ item.gameName }}</span>
            <div class="author-box">
              <div class="icon"><van-icon name="manager-o" class="icon" /></div>
              <span class="author">{{ item.author }}</span>
              <span class="like">
                <van-icon name="good-job-o" />
                <span class="count">{{ item.connect }}</span>
              </span>
            </div>
          </div>
        </div>
      </div>
    </van-pull-refresh>

    <van-overlay :show="isPlaying" @click="closePlay">
      <div class="wrapper">
        <video :src="videoSrc" controls ref="videoPlayer" class="video_player" autoplay></video>
      </div>
    </van-overlay>
  </div>
</template>

<script setup>
import axios from 'axios'
import { useRoute } from 'vue-router'
import { onMounted, ref } from 'vue'

const route = useRoute()

const loading = ref(false)
const rLoading = ref(false)
const data = ref([])
const listBox = ref(null)
const videoSrc = ref('')
const isPlaying = ref(false)
const videoPlayer = ref(null)

const getData = function () {
  loading.value = true

  axios
    .get('/api/data/getList', {
      params: {
        id: route.query.id,
      },
    })
    .then((res) => {
      data.value = res.data.data
      rLoading.value = false
      loading.value = false
    })
    .catch(() => {
      rLoading.value = false
      loading.value = false
    })
}

const getMoreData = function () {
  loading.value = true

  axios
    .get('/api/data/getList', {
      params: {
        id: route.query.id,
      },
    })
    .then((res) => {
      data.value = [...data.value, ...res.data.data]
      loading.value = false
    })
    .catch(() => {
      loading.value = false
    })
}

onMounted(() => {
  getData()
})

const onRefresh = function () {
  getData()
}

// 方法：启动播放
function startPlayback(item) {
  if (item.type !== 'video') return
  videoSrc.value = getUrl(item.videoUrl)
  isPlaying.value = true
  if (videoPlayer.value) {
    videoPlayer.value.play() // 开始播放视频
  }
}

function closePlay() {
  if (videoPlayer.value) {
    videoPlayer.value.pause() // 停止播放
  }
  isPlaying.value = false
  videoSrc.value = ''
}

function scroll() {
  const scrollHeight = listBox.value.scrollHeight || 0
  const clientHeight = listBox.value.clientHeight || 0
  const scrollTop = listBox.value.scrollTop || 0
  if (scrollHeight - clientHeight - scrollTop < 10 && !loading.value) {
    getMoreData()
  }
}

function getUrl(url) {
  if (url) {
    return new URL(url, import.meta.url).href
  }
}

// const onLoad = async function () {
//   loadCount.value++
//   // if (loadCount.value < 5) {
//   //   await getData()
//   //   loading.value = false
//   // } else {
//   //   finish.value = true
//   // }
//   await getData()
//   loading.value = false
// }
</script>

<style lang="less" scoped>
.home {
  width: 100%;
  height: 100vh;
  background: #fcf8f8;
  display: flex;
  overflow-x: hidden;
  justify-content: space-between;
  flex-wrap: wrap;
  position: relative;
}

.wrapper {
  width: 100vw;
  height: 100vh;
  position: relative;

  .video_player {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    width: 100%;
  }
}

.list-box {
  width: 100%;
  height: 100%;
  overflow-y: auto;
  display: grid;
  grid-auto-rows: minmax(1px, 1px);
  grid-template-columns: repeat(2, 1fr);
  column-gap: 8px;
}

.item-box {
  // width: 48%;
  background: #fff;
  margin-bottom: 8px;
  display: flex;
  flex-direction: column;
  .player {
    position: absolute;
    top: 4px;
    right: 4px;
    width: 24px;
    height: 24px;
    border-radius: 50%;
    background: rgba(31, 31, 31, 0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    color: #fff;
  }
}

.img {
  width: 100%;
  position: relative;
}

.info-box {
  flex: 1;
  padding: 8px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  .title-box {
    // 展示两行，溢出显示省略号
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    font-size: 14px;
  }
  .game-name {
    width: fit-content;
    padding: 4px;
    border-radius: 8px;
    background: #ccc;
    color: gray;
    font-size: 12px;
  }
  .author-box {
    width: 100%;
    display: flex;
    align-items: center;
    font-size: 14px;
    column-gap: 4px;
    .icon {
      i {
        margin-top: 4px;
      }
    }
    .author {
      flex: 1;
      overflow: hidden;
      text-overflow: ellipsis;
      display: flex;
      align-items: center;
    }
    .like {
      margin-left: auto;
      display: flex;
      align-items: center;
      column-gap: 4px;
    }
  }
}
</style>
